/***************************************************************************
 *            object_3d.c
 *
 *  Mon Aug 25 17:40:14 2008
 *  Copyright  2008  Tiago Falcão
 *  <tiago@tiagofalcao.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
#include "object_3d.h"

// Esfera Simplificada
void simple_glutSolidSphere(GLdouble size){
	glutSolidSphere(size/2, 30, 30);
}
void simple_glutWireSphere(GLdouble size){
	glutWireSphere(size/2, 30, 30);
}

// Desenha Objeto 3D
void obj3d_display(object3d * obj){
		glPushMatrix ();
		//Posiciona
		glTranslatef (obj->position.x, obj->position.y, obj->position.z);
		//Rotaciona
		glRotatef (obj->rotate.x, 1.0, 0.0, 0.0);
		glRotatef (obj->rotate.y, 0.0, 1.0, 0.0);
		glRotatef (obj->rotate.z, 0.0, 0.0, 1.0);
		//Alfa
		if (obj->color.a<1.0){
			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		}
		//Cor
		glColor4f(obj->color.r,obj->color.g,obj->color.b,obj->color.a);
		//Escala
		glScalef(obj->scale.x,obj->scale.y,obj->scale.z);
		//Brilho
		glMaterialfv(GL_FRONT, GL_AMBIENT, obj->ambient);		//Ambiente
		glMaterialfv(GL_FRONT, GL_DIFFUSE, obj->diffuse);		//Difusao
		glMaterialfv(GL_FRONT, GL_SPECULAR, obj->specular);		//Reflexao
		glMaterialf(GL_FRONT, GL_SHININESS, obj->shininess);	//Concentracao
		glMaterialfv(GL_FRONT, GL_EMISSION, obj->emission);	 	//Emissao
		//Desenho do Objeto
		if(obj->selected)
			obj->display_selected(obj->size);
		else
			obj->display(obj->size);
		
		glPopMatrix ();
}

//Translacao em X
void obj3d_trans_x(object3d * obj,short int sign){
	obj->position.x+=sign*0.5;
}
//Translacao em Y
void obj3d_trans_y(object3d * obj,short int sign){
	obj->position.y+=sign*0.5;
}
//Translacao em Z
void obj3d_trans_z(object3d * obj,short int sign){
	obj->position.z+=sign*0.5;
}

//Rotacao no eixo X
void obj3d_rot_x(object3d * obj,short int sign){
	obj->rotate.x+=sign*10.0;
}
//Rotacao no eixo Y
void obj3d_rot_y(object3d * obj,short int sign){
	obj->rotate.y+=sign*10.0;
}
//Rotacao no eixo Z
void obj3d_rot_z(object3d * obj,short int sign){
	obj->rotate.z+=sign*10.0;
}

//Tamanho
void obj3d_size(object3d * obj,short int sign){
	obj->size+=sign*0.05;
	if(obj->size<=0.05)
		obj->size=0.05;
}

//Escala em X
void obj3d_scale_x(object3d * obj,short int sign){
	obj->scale.x+=sign*0.1;
	if(obj->scale.x<=0.1)
		obj->scale.x=0.1;
}
//Escala em Y
void obj3d_scale_y(object3d * obj,short int sign){
	obj->scale.y+=sign*0.1;
	if(obj->scale.y<=0.1)
		obj->scale.y=0.1;
}
//Escala em Z
void obj3d_scale_z(object3d * obj,short int sign){
	obj->scale.z+=sign*0.1;
	if(obj->scale.z<=0.1)
		obj->scale.z=0.1;
}
